<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 * 
 * 创建时间：2021-05-17 11:01
 *
 * 项目：upload  -  $  - FormsTable.php
 *
 * 作者：liwei 
 */

namespace modules\levfm\table;

use Lev;
use lev\base\Modelv;
use lev\helpers\dbHelper;
use modules\levfm\table\levfm_datas\BaseLevfmDatasModel;

!defined('INLEV') && exit('Access Denied LEV');


class LevfmDatas extends BaseLevfmDatasModel
{
    public static $tableName = '{{%levfm_datas}}';

    public static function status() {
        return [
            0 => '正常',
            1 => '关闭',
            2 => '未审核',
            3 => '未公开',
        ];
    }

    /**
     * 格式化唯一字段入库字串
     * @param $uniques
     * @return false|string
     */
    public static function setUniques($uniques) {
        return LevfmDatasChild::setSrhtype($uniques);
    }

    /**
     * 检查字段值是否唯一
     * @param $fieldid
     * @param $value
     * @param int $opid
     * @return array|mixed
     */
    public static function checkUnique($fieldid, $value, $opid = 0, $formid = 0) {
        $string = LevfmDatasChild::setSrhString($fieldid, addcslashes($value, '%_\'"'));
        $where[] = "uniques LIKE '[%{$string}%'";
        $formid >0 && $where[] = 'formid='.$formid;
        $opid >0   && $where[] = '(id>'.$opid.' OR id<'.$opid.')';
        return static::findOne(implode(' AND ', $where), 'id');
    }

    /**
     * 筛选字串标题
     * @param $datas
     * @param $fields
     * @return mixed|string
     */
    public static function getDatasTitle($datas, $fields) {
        $string = '';
        if ($datas) {
            !is_array($datas) && $datas = static::getDatas($datas);
            foreach ($datas as $inputname => $inputvalue) {
                if ($fields[$inputname]['inputtype'] == 'text') {
                    $string .= $inputvalue;
                }
            }
        }
        return $string;
    }

    public static function setDatas(array $datas)
    {
        return Lev::setSettings($datas);
    }

    public static function getDatas($datas, $key = false, $val = false) {
        return Lev::getSettings($datas, $key, $val);
    }

    public static function adminDelete($optab = '')
    {
        $msg = parent::adminDelete($optab); // TODO: Change the autogenerated stub
        $msg['child'] = LevfmDatasChild::adminDelete();
        $msg['message'] .= ' | '.$msg['child']['message'];
        return $msg;
    }

    public static function setStatus($optab = '', $upData = [])
    {
        $opid = intval(Lev::GPv('opid'));
        $status = intval(Lev::GPv('status')) ? 1 : 0;

        static::setDatasChildStatus($status, $opid);

        return parent::setStatus($optab, $upData); // TODO: Change the autogenerated stub
    }

    public static function setField($optab = '', $val = null, $upData = [])
    {
        $field = Lev::stripTags(Lev::GPv('field'));
        LevfmDatasChild::safeColumnsGen([$field=>1]) &&
        LevfmDatasChild::setField($optab, $val, $upData);
        if ($field === 'status') {
            $opid = intval(Lev::GPv('opid'));
            $status = intval(Lev::GPv('val'));

            static::setDatasChildStatus($status, $opid);

        }
        return parent::setField($optab, $val, $upData); // TODO: Change the autogenerated stub
    }

    public static function setDatasChildStatus($status, $opid) {

        if ($status) {
            LevfmDatasChild::delete(['id'=>$opid]);
        }else {
            LevfmDatasChild::saveChild(static::findOne(['id'=>$opid]));
        }

    }
}